package com.juntuo.controller;

import com.github.pagehelper.PageInfo;
import com.juntuo.entity.Company;
import com.juntuo.entity.Role;
import com.juntuo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/index")
    public String toIndex(){
        return "/role/index";
    }

    @RequestMapping("/page")
    @ResponseBody
    public PageInfo<Role> getPage(int pageNumber, int pageSize){
        return roleService.findPagedRole(pageNumber,pageSize);
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Role> getPage(){
        return roleService.selectAll();
    }

    @RequestMapping("/save")
    @ResponseBody
    public boolean saveOne(Role role){
        if(role.getId()==null || role.getId()==0){
            roleService.insert(role);
        }else{
            roleService.updateByPrimaryKeySelective(role);
        }
        return true;
    }


    @RequestMapping("/delete/{id}")
    @ResponseBody
    public int deleteOne(@PathVariable int id){
        Role role = new Role();
        role.setId(id);
        roleService.delete(role);
        return 1;
    }
}
